package com.turnengine.client.local.unit.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.unit.enums.UnitMove;
import com.turnengine.client.local.unit.enums.UnitNode;
import com.turnengine.client.local.unit.enums.UnitType;
import java.io.IOException;

/**
 * The Unit Serializer.
 */
public class UnitSerializer implements IUnitSerializer {

	@Override
	public IUnit readObject(IDataReader reader) throws IOException {

		// Read fields
		int id = reader.readInt();
		String name = reader.readString(true);
		UnitNode node = reader.readEnum(UnitNode.class, true);
		UnitType type = reader.readEnum(UnitType.class, true);
		UnitMove move = reader.readEnum(UnitMove.class, true);
		int groupId = reader.readInt();
		int factionId = reader.readInt();

		// Create object
		IUnit object = new Unit();
		object.setId(id);
		object.setName(name);
		object.setNode(node);
		object.setType(type);
		object.setMove(move);
		object.setGroupId(groupId);
		object.setFactionId(factionId);
		return object;
	}

	public void writeObject(IDataWriter writer, IUnit object) throws IOException {

		// Get fields
		int id = object.getId();
		String name = object.getName();
		UnitNode node = object.getNode();
		UnitType type = object.getType();
		UnitMove move = object.getMove();
		int groupId = object.getGroupId();
		int factionId = object.getFactionId();

		// Write fields
		writer.writeInt(id);
		writer.writeString(name, true);
		writer.writeEnum(node, true);
		writer.writeEnum(type, true);
		writer.writeEnum(move, true);
		writer.writeInt(groupId);
		writer.writeInt(factionId);
	}
}
